package MA_Projekt_Client_SL;

import java.util.List;

import de.unidue.masterproject.ccc.exceptions.WebServiceException;
import de.unidue.masterproject.generalinterfaces.IWebServices;
import de.unidue.masterproject.generalmodel.WebserviceType;

public class WebServiceFacade extends DefaultRESTController implements IWebServices {

	public WebServiceFacade() {
		withAuthorization = true;
	}

	/* (non-Javadoc)
	 * @see MA_Projekt_Client_SL.IWebServiceFacade#get(MA_Projekt_Client_SL.WebserviceType, java.lang.Class, java.lang.String, java.lang.String)
	 */
	@Override
	public <T> T get(WebserviceType wsType, Class<T> entityClass,
			String plugInName, String plugInVersion, String username, String password) throws WebServiceException {
		setUserName(username);
		setPassword(password);
		return httpGet(
				buildPath(wsType, entityClass.getSimpleName(), plugInName,
						plugInVersion), entityClass);
	}

	/* (non-Javadoc)
	 * @see MA_Projekt_Client_SL.IWebServiceFacade#getList(MA_Projekt_Client_SL.WebserviceType, java.lang.Class, java.lang.String, java.lang.String)
	 */
	@Override
	public <T> List<T> getList(WebserviceType wsType, Class<T> entityClass,
			String plugInName, String plugInVersion, String username, String password) throws WebServiceException {
		setUserName(username);
		setPassword(password);return httpGetCollection(
				buildPath(wsType, entityClass.getSimpleName(), plugInName,
						plugInVersion), entityClass);
	}

	/* (non-Javadoc)
	 * @see MA_Projekt_Client_SL.IWebServiceFacade#delete(MA_Projekt_Client_SL.WebserviceType, java.lang.Class, java.lang.String, java.lang.String)
	 */
	@Override
	public <T> void delete(WebserviceType wsType, Class<T> entityClass,
			String plugInName, String plugInVersion, String username, String password) throws WebServiceException {
		setUserName(username);
		setPassword(password);
		httpDelete(
				buildPath(wsType, entityClass.getSimpleName(), plugInName,
						plugInVersion), entityClass);
	}

	/* (non-Javadoc)
	 * @see MA_Projekt_Client_SL.IWebServiceFacade#put(MA_Projekt_Client_SL.WebserviceType, T, java.lang.Class, java.lang.String, java.lang.String)
	 */
	@Override
	public <T> void put(WebserviceType wsType, T object, Class<T> entityClass,
			String plugInName, String plugInVersion, String username, String password) throws WebServiceException {
		setUserName(username);
		setPassword(password);
		httpPutUpdate(
				buildPath(wsType, entityClass.getSimpleName(), plugInName,
						plugInVersion), object, entityClass);
	}

	/* (non-Javadoc)
	 * @see MA_Projekt_Client_SL.IWebServiceFacade#post(MA_Projekt_Client_SL.WebserviceType, T, java.lang.Class, java.lang.String, java.lang.String)
	 */
	@Override
	public <T> void post(WebserviceType wsType, T object, Class<T> entityClass,
			String plugInName, String plugInVersion, String username, String password) throws WebServiceException {
		setUserName(username);
		setPassword(password);httpPost(
				buildPath(wsType, entityClass.getSimpleName(), plugInName,
						plugInVersion), object, entityClass);
	}

	private String buildPath(WebserviceType wsType, String entityName,
			String plugInName, String plugInVersion) {
		String path = "";
		if (wsType == WebserviceType.GENERIC) {
			path += "/genericWS";
		}
		path += "/" + plugInName + "" + plugInVersion + "/" + entityName;
		return path;
	}

}
